#include <QtGui/QMessageBox>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnAbout_clicked()
{
    QMessageBox::about(this,"About DictGen",
            "This app was coded for educational purposes.\n"
            "Bye.\n");
}

void MainWindow::LogMessage(QString message)
{
    QString str = ui->txtLog->toPlainText();
    ui->txtLog->setText(str + "\n" + message);
}

bool MainWindow::createConnection(QString databaseName, QString username, QString password)
{
    db = QSqlDatabase::addDatabase("QMYSQL","MySql");
    //db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName(databaseName);
    db.setUserName(username);
    db.setPassword(password);
    if (!db.open())
    {
        //QMessageBox::critical(0, QObject::tr("Database Error"),db.lastError().text());
        return false;
    }
    return true;
}

bool MainWindow::initSqliteConnection(QString databaseName)
{
    db = QSqlDatabase::addDatabase("QSQLITE","Sqlite");
    db.setDatabaseName(databaseName);
    if (!db.open())
    {
        //QMessageBox::critical(0, QObject::tr("Database Error"),db.lastError().text());
        return false;
    }
    return true;
}

void MainWindow::on_btnMYSqlConnect_clicked()
{
    QString username = ui->txtUsername->text();
    QString password = ui->txtPassword->text();
    QString database = ui->txtDatabase->text();

    this->LogMessage("Read: database = " + database + ", username = " + username + ", password = " + password);

    if (this->createConnection(database,username,password))
    {
        this->LogMessage("Connected to " + database);
    }
    else
    {
        this->LogMessage("Error !!! " + db.lastError().text());
    }

    //Listing table
    QStringList table = db.tables(QSql::Tables);
    QString t;
    foreach (t,table)
    {
        this->LogMessage(t);
    }

    //Listing first 10 records of table 1
    QFile data("output.txt");
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForTr(codec);

    if (data.open(QFile::WriteOnly | QFile::Truncate)) {
        QTextStream out(&data);
        out.setCodec(codec);
        this->LogMessage("File opened ");

        db = QSqlDatabase::database("MySql");
        QSqlQuery query(db);

        db.exec("SET NAMES 'utf8'");

        QString strQuery = "SELECT word_word, word_meaning FROM dict_english_vietnamese_general LIMIT 32,1";
        //QString strQuery = "SELECT latin,utf8 FROM test";
        this->LogMessage(strQuery);

        QByteArray byteMeaning;
        QString meaning;
        QString word;

        QByteArray test;

        if (query.exec(strQuery))
        {
            while (query.next())
            {
                word = query.value(0).toString().toUtf8();

/*                meaning = query.value(1).toString();
                out << "--------------meaning1--------------------\n";
                out << meaning;
                this->LogMessage(meaning);

                out << "\n\n--------------1--------------------\n";
                meaning = query.value(1).toString().toUtf8();
                out << meaning;
                this->LogMessage(meaning);

                out << "\n\n--------------2--------------------\n";
                meaning = query.value(1).toString().toLatin1();
                out << meaning;
                this->LogMessage(meaning);*/

                out << "\n\n--------------3--------------------\n";
                meaning = codec->toUnicode(query.value(1).toByteArray());
                out << meaning;
                this->LogMessage(meaning);

                out << "\n\n--------------5--------------------\n";
                byteMeaning = query.value(1).toByteArray();
                meaning = QString::fromUtf8(byteMeaning);
                out << meaning;
                this->LogMessage(meaning);
            }
        }
        else
        {
            this->LogMessage("Error !!! " + query.lastError().text());
        }
/*        strQuery = "INSERT INTO test (utf8,latin) VALUES ('thử nghiệm tiếng việt xem hiển thị có đúng kô11','thử nghiệm tiếng việt xem hiển thị có đúng kô11')";
        if (query.exec(strQuery))
            this->LogMessage("OK");
        else
            this->LogMessage("Error");*/
    }
    else
    {
        this->LogMessage("Error!!!");
    }
    data.close();
}

void MainWindow::on_btnExportSqlite_clicked()
{
    if (this->initSqliteConnection("test.db"))
    {
        this->LogMessage("Connected to test.db" );
    }
    else
    {
        this->LogMessage("Error !!! " + db.lastError().text());
    }
  QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForTr(codec);
    db = QSqlDatabase::database("Sqlite",true);
    QSqlQuery query(db);
    QString strQuery = "INSERT INTO anhviet (word,meaning) VALUES ('thử nghiệm','thử nghiệm tiếng việt xem hiển thị có đúng kô11')";
    query.exec("CREATE TABLE anhviet  (id int primary key, word varchar(255), meaning text)");
    query.exec(strQuery);
    //query.exec("INSERT INTO anhviet (word,meaning) VALUES 'test','testset'");
}
